package crp.core.workflow.domain.models;

import com.qwlabs.lang.C2;
import lombok.Getter;
import lombok.Setter;

import java.util.List;
import java.util.Objects;
import java.util.Optional;

@Getter
@Setter
public class WorkflowDefinition {
    private List<WorkflowDefinitionNode> nodes;

    public Optional<WorkflowDefinitionNode> find(String nodeId) {
        return C2.stream(nodes)
                .filter(node -> Objects.equals(node.getId(), nodeId))
                .findFirst();
    }

    public Optional<WorkflowDefinitionNode> next(String nodeId) {
        if (Objects.isNull(nodeId)) {
            return C2.stream(nodes).findFirst();
        }
        return find(nodeId)
                .map(node -> {
                    int nextIndex = nodes.indexOf(node) + 1;
                    if (nextIndex >= nodes.size()) {
                        return null;
                    }
                    return nodes.get(nextIndex);
                });
    }
}
